{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.common import PayReceive, Currency\n",
    "from gs_quant.instrument import IRSwaption\n",
    "from gs_quant.risk import CurveScenario, MarketDataPattern\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "swaption = IRSwaption(PayReceive.Receive, '5y', Currency.USD, expiration_date='13m', strike='atm')\n",
    "swaption.resolve()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "original_price = swaption.price()\n",
    "# retrieve the market data our instrument is sensitive to.\n",
    "market_data = swaption.market().market_data_dict\n",
    "print('Base price:     {:,.2f}'.format(original_price))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pivot point is the tenor at which curve shift =0 and influences the type and shape of curve shift\n",
    "# Price the swaption under a bull steepener scenario of 10bp\n",
    "bull_steepener_scenario = CurveScenario(\n",
    "    market_data_pattern=MarketDataPattern('IR', 'USD'), curve_shift=10, tenor_start=15, tenor_end=25, pivot_point=25\n",
    ")\n",
    "\n",
    "with bull_steepener_scenario:\n",
    "    swaption_bull_steepener = swaption.price()\n",
    "    market_data_bull_steepener = swaption.market().market_data_dict\n",
    "\n",
    "print('Price under bull steepener curve shift: {:,.2f}'.format(swaption_bull_steepener))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compare swap rate market data coordinates before and after curve scenario shock\n",
    "market_data_df = pd.DataFrame(\n",
    "    [\n",
    "        {\n",
    "            mkt_data: value * 1e4\n",
    "            for mkt_data, value in market_data.items()\n",
    "            if (mkt_data.mkt_type == \"IR\" and mkt_data.mkt_asset == \"USD\" and mkt_data.mkt_class == \"SWAP OIS\")\n",
    "        },\n",
    "        {\n",
    "            mkt_data: value * 1e4\n",
    "            for mkt_data, value in market_data_bull_steepener.items()\n",
    "            if (mkt_data.mkt_type == \"IR\" and mkt_data.mkt_asset == \"USD\" and mkt_data.mkt_class == \"SWAP OIS\")\n",
    "        },\n",
    "    ],\n",
    "    index=['Values', 'Shocked values'],\n",
    ").transpose()\n",
    "market_data_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plotting swap rate market data before and after curve scenario shock\n",
    "swap_curve = pd.DataFrame.from_dict(\n",
    "    {int(''.join(list(filter(str.isdigit, str(v))))): market_data_df.loc[v] for v in market_data_df.index},\n",
    "    orient='index',\n",
    ")\n",
    "\n",
    "swap_curve['Shock'] = swap_curve['Shocked values'] - swap_curve['Values']\n",
    "swap_curve.plot(\n",
    "    figsize=(12, 8),\n",
    "    title='USD Swap Curve Before and After {}bp bull Steepening Shock'.format(bull_steepener_scenario.curve_shift),\n",
    ")\n",
    "plt.xlabel('Tenor (years)')\n",
    "plt.ylabel('bp')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
